package com.licq.ldoc.utils;

import java.awt.Color;

import com.lowagie.text.Anchor;
import com.lowagie.text.Cell;
import com.lowagie.text.Font;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.BaseFont;

/**
 * 
 * @author liujian
 *
 */
public class LowAgieUtils {
	
	private static final String FONT_NAME = "STSongStd-Light";

	private static final String ENCODING = "UniGB-UCS2-H";
	
	public static final Font DEFAULT_FONT;
	
	public static final Font BLUE_FONT;
	
	public static final Font BOLD_FONT;
	
	public static final Font DEFAULT_LINK_FONT;
	
	public static final Font BOLD_LINK_FONT;
    
    static {
    	try {
    		DEFAULT_FONT = new Font(BaseFont.createFont(FONT_NAME, ENCODING, 
    				BaseFont.NOT_EMBEDDED));
    		BLUE_FONT = new Font(DEFAULT_FONT);
    		BLUE_FONT.setColor(Color.BLUE);
    		BOLD_FONT = new Font(DEFAULT_FONT);
    		BOLD_FONT.setFamily(BaseFont.HELVETICA_BOLD);
			BOLD_FONT.setStyle(Font.BOLD);
			DEFAULT_LINK_FONT = new Font(BaseFont.createFont(FONT_NAME, ENCODING, 
					BaseFont.NOT_EMBEDDED));
			DEFAULT_LINK_FONT.setColor(Color.BLUE);
			DEFAULT_LINK_FONT.setStyle(Font.UNDERLINE | Font.ITALIC);
			BOLD_LINK_FONT = new Font(DEFAULT_LINK_FONT);
			BOLD_LINK_FONT.setFamily(BaseFont.HELVETICA_BOLD);
			BOLD_LINK_FONT.setStyle(Font.BOLD);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
    }
    
    public static Cell createTableHeader(String text) {
    	Cell cell = createCell(text, Cell.ALIGN_CENTER, Cell.ALIGN_MIDDLE, true);
    	cell.setBackgroundColor(new Color(204, 204, 204));
		return cell;
    }
    
    public static Anchor createAnchor(String text, boolean bold) {
    	return new Anchor(text, bold ? BOLD_FONT : DEFAULT_FONT);
    }
    
    public static Anchor createLink(String text, boolean bold) {
    	return new Anchor(text, bold ? BOLD_LINK_FONT : DEFAULT_LINK_FONT);
    }
    
    public static Cell createCell(String text) {
    	return createCell(text, false);
    }
	
	public static Cell createCell(String text, boolean bold) {
    	return createCell(text, Cell.ALIGN_LEFT, Cell.ALIGN_TOP, bold);
    }
	
	public static Cell createHCell(String text, int hAlign) {
		return createCell(text, hAlign, Cell.ALIGN_TOP, false);
	}
	
	public static Cell createVCell(String text, int vAlign) {
		return createCell(text, Cell.ALIGN_LEFT, vAlign, false);
	}
    
    public static Cell createCell(String text, int hAlign, int vAlign, boolean bold) {
    	Cell cell = new Cell();
    	cell.setHorizontalAlignment(hAlign);
    	cell.setVerticalAlignment(vAlign);
    	cell.add(createPhrase(text, bold));
    	return cell;
    }
    
    public static Phrase createPhrase(String text, boolean bold) {
    	Phrase phrase = new Phrase();
    	phrase.setFont(bold ? BOLD_FONT : DEFAULT_FONT);
    	phrase.add(text);
    	return phrase;
    }

}
